package com.hk.userservice.controller;

import com.github.pagehelper.PageInfo;
import com.hk.commons.CommonResult;
import com.hk.model.User;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import com.hk.userservice.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/2 9:38
 * @Statement: 会员信息
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 分页模糊或查询所有会员
     * @param page 当前页
     * @param limit 每页显示数量
     * @param user 模糊查询数据
     * @return
     */
    @GetMapping("/findAll")
    public ResultVO<User> findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10")Integer limit,
            User user
    ) {
        List<User> userList = userService.findAll(page, limit, user);
        PageInfo<User> userPageInfo = new PageInfo<>(userList);
        int count = userService.count(-1,-1, user);
        if (userList.isEmpty()) {
            ResultVO resultVO = CommonResult.nullResultVO(count,userPageInfo.getList());
            return resultVO;
        }
        ResultVO resultVO = CommonResult.successResultVO(count,userPageInfo.getList());
        return resultVO;

    }

    /**
     * 邮箱校验
     * @param email 原始邮箱
     * @return
     */
    @GetMapping("/findByEmail")
    public Result findByEmail(@RequestParam(required = false) String email) {
        if (email == null || email == "") {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        int email1 = userService.findByEmail(email);
        if (email1 == 1) {
            Result result = CommonResult.successResult(null);
            return result;
        }
        Result result = CommonResult.nullResult(null);
        return result;
//        return null;
    }

    /**
     * 添加会员信息
     * @param user 会员信息
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody(required = false) User user) {
        if (user == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        userService.save(user);
        Result result = CommonResult.successResult(null);
        return result;
//        return null;
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id) {
        User user = userService.findById(id);
        if (user.getId() == 0) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        Result result = CommonResult.successResult(user);
        return result;
//        return null;
    }

    @PostMapping("/updateMessage")
    public Result updateMessage(@RequestBody(required = false) User user) throws IOException {
        if (user == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        userService.update(user);
        Result result = CommonResult.successResult(null);
        return result;
    }

    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody(required = false) User user) {
        if (user == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        userService.updatePassWord(user);
        Result result = CommonResult.successResult(null);
        return result;
    }

    @PostMapping("/updateState")
    public Result updateState(@RequestBody(required = false) User user) {
        if (user == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        userService.updateState(user);
        Result result = CommonResult.successResult(null);
        return result;
    }

    @PostMapping("/deletetUser/{id}")
    public Result deleteUser(@PathVariable int id) throws IOException {
        userService.delete(id);
        Result result = CommonResult.successResult(null);
        return result;
    }

}
